﻿Imports XL.Common
Imports XL.Common.Utils
Imports AssetVisionXAuto.Facade
Imports System.Collections
Imports System.Data

Namespace Facade
    Public Class OpBizMaster

        Public Shared Function GenerateAssetClassifyIDPath(ByVal classifyID As String, ByRef classifyDepth As Integer) As String

            Dim result As String = String.Empty
            Dim classifyRow As AssetVisionXAuto.FTs.FT_M_AV_ASSET_CLASSIFYRow
            Dim classifyCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_ASSET_CLASSIFY(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim classifyIDPath As New ArrayList

            classifyDepth = 0
            Do While classifyID.Length > 0

                classifyIDPath.Add(classifyID)
                classifyCondition = New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_ASSET_CLASSIFY(XL.DB.Utils.Condition.LogicOperators.Logic_And)
                classifyCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.M_AV_ASSET_CLASSIFYColumns.CLASSIFY_IDColumn, "=", classifyID)
                classifyRow = _
                    AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_ASSET_CLASSIFYRow(classifyCondition)

                If IsNothing(classifyRow) = True Then
                    Return String.Empty
                End If

                classifyDepth += 1
                classifyID = classifyRow.PARENT_CLASSIFY

            Loop

            Dim counter As Integer

            For counter = classifyIDPath.Count - 1 To 0 Step -1
                result &= CommTK.FString(classifyIDPath(counter)) & "/"
            Next

            result = "/" & result

            Return result

        End Function

        Public Shared Function GenerateLocationIDPath(ByVal locationID As String, ByRef locationDepth As Integer) As String

            Dim result As String = String.Empty
            Dim locationRow As AssetVisionXAuto.FTs.FT_M_AV_LOCATIONRow
            Dim locationCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim locationIDPath As New ArrayList

            locationDepth = 0
            Do While locationID.Length > 0

                locationIDPath.Add(locationID)
                locationCondition = New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
                locationCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.M_AV_LOCATIONColumns.LOCATION_IDColumn, "=", locationID)
                locationRow = _
                    AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_LOCATIONRow(locationCondition)

                If IsNothing(locationRow) = True Then
                    Return String.Empty
                End If

                locationDepth += 1
                locationID = locationRow.PARENT_ID

            Loop

            Dim counter As Integer

            For counter = locationIDPath.Count - 1 To 0 Step -1
                result &= CommTK.FString(locationIDPath(counter)) & "/"
            Next

            result = "/" & result

            Return result

        End Function

        Public Shared Sub SetChildAssetsNoInventory(ByVal assetIDs As ArrayList, ByVal bindingList As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSET)

            Dim childAssetList As New AssetVisionXAuto.FTs.FT_T_AV_ASSET
            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim bindingCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            Do
                assetCondition.Add(AfBizMaster.T_AV_ASSETColumns.PARENT_ASSETColumn, True, assetIDs)
                AssetVisionXAuto.Facade.AfBizMaster.FillFT_T_AV_ASSET(assetCondition, childAssetList)

                assetIDs = _
                    AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSET_CVListDistinct( _
                        assetCondition, _
                        AfBizMaster.T_AV_ASSETColumns.ASSET_IDColumn)

                For Each bindingRow As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSETRow In bindingList.FindRowsByCondition(bindingCondition)
                    bindingRow.INVENTORY_REQUIRED = False
                Next

            Loop While assetIDs.Count > 0

            bindingCondition.Clear()
            bindingCondition.Add(AfXV.XV_T_AV_ASSETColumns.ASSET_IDColumn, True, assetIDs)
            For Each childAssetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow In childAssetList
                childAssetRow.INVENTORY_REQUIRED = False
            Next

            AssetVisionXAuto.Facade.AfBizMaster.SaveBatchT_AV_ASSETData(childAssetList)
        End Sub

        Public Shared Sub FillAddressAloneDepartmentLocationListByStaffID( _
            ByVal staffID As String, _
            ByVal departmentID As String, _
            ByRef rootDepartmentRowSEntity As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRowSEntity, _
            ByRef locationList As AssetVisionXAuto.FTs.FT_M_AV_LOCATION)

            Dim staffRow = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_STAFFRow(staffID)

            Dim locationCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            If AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL = 0 Then

                locationCondition.Add("Len(IsNull(DEPARTMENT_ID, '')) > 0")
                AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_LOCATION( _
                    locationCondition, _
                    locationList)
                Return

            ElseIf staffID.Length > 0 AndAlso IsNothing(staffRow) = True Then

                If IsNothing(staffRow) = True Then
                    Dim ex As New XLException(XLException.ErrorClassify.ReadDataError)
                    ex.SetMessage(AssetVisionXService.Decls.MSG_ALERT_00035)
                    Throw ex
                End If

                AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_DEPARTMENTRowSEntity(rootDepartmentRowSEntity, staffRow.DEPARTMENT_ID)
            ElseIf departmentID.Length > 0 Then
                AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_DEPARTMENTRowSEntity(rootDepartmentRowSEntity, departmentID)
            Else
                Dim ex As New XLException(XLException.ErrorClassify.ReadDataError)
                ex.SetMessage(AssetVisionXService.Decls.MSG_ALERT_00034)
                Throw ex
            End If

            If rootDepartmentRowSEntity.IsNull = True Then
                Return
            End If

            Do While rootDepartmentRowSEntity.DEPARTMENT_DEPTH > AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL
                AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_DEPARTMENTRowSEntity(rootDepartmentRowSEntity, rootDepartmentRowSEntity.PARENT_DEPARTMENT)
            Loop

            Dim departmentCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            departmentCondition.Add(String.Format("DEPARTMENT_ID_PATH LIKE '%/{0}/%'", rootDepartmentRowSEntity.DEPARTMENT_ID))

            Dim departmentIDs As ArrayList = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENT_CVListDistinct(departmentCondition, AfBizMaster.M_AV_DEPARTMENTColumns.DEPARTMENT_IDColumn)
            locationCondition.Clear()
            locationCondition.Add(AfBizMaster.M_AV_LOCATIONColumns.DEPARTMENT_IDColumn, True, departmentIDs)

            AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_LOCATION( _
                locationCondition, _
                locationList)

        End Sub

        Public Shared Sub SetDepartmentRunner( _
            ByVal departmentID As String, _
            ByVal runnerStaffID As String, _
            ByRef refLoginStaffRowEntity As AssetVisionXAuto.FTs.FT_XV_M_AV_STAFFRowSEntity)

            Dim departmentRow = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentID)
            Dim departmentCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            If AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL > 0 Then

                Do While IsNothing(departmentRow) = False AndAlso departmentRow.DEPARTMENT_DEPTH > AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL
                    departmentRow = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentRow.PARENT_DEPARTMENT)
                Loop

                departmentCondition.Add(String.Format("DEPARTMENT_ID_PATH Like '%/{0}/%'", departmentRow.DEPARTMENT_ID))
            Else
                SysInfo.WriteShareSysInfo(AssetVisionXService.Decls.SVN_ADMIN_ALONE_STAFF_ID, runnerStaffID)
                Dim staffCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_M_AV_STAFF(XL.DB.Utils.Condition.LogicOperators.Logic_And)
                staffCondition.Add(AfXV.XV_M_AV_STAFFColumns.STAFF_IDColumn, "=", runnerStaffID)
                AssetVisionXAuto.Facade.AfXV.FillXV_M_AV_STAFFRowSEntity(refLoginStaffRowEntity, staffCondition)
            End If

            AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_DEPARTMENTData( _
                departmentCondition, _
                AssetVisionXAuto.Facade.AfBizMaster.M_AV_DEPARTMENTColumns.RUNNERColumn, _
                runnerStaffID)

            If CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_ADDRESS_ALONE_DEPT_LEVEL)) = 0 Then
                Dim userCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.Condition.LogicOperators.Logic_And)
                userCondition.Add(AssetVisionXAuto.Facade.AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", CommDecl.SYSTEM_DEFAULT_ACCOUNT)
                Dim userRowSE As New AssetVisionXAuto.FTs.FT_S_USERRowSEntity
                AssetVisionXAuto.Facade.AfSysConfig.FillS_USERRowSEntity(userRowSE, userCondition)
                AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_STAFFRow(runnerStaffID, AfBizMaster.M_AV_STAFFColumns.USER_IDColumn, userRowSE.USER_ID)
            End If

            Facade.OpMP.ExecuteMP_AV_EXECUTE_SYNC_ASSET_RUNNER_BY_LOCATION()

        End Sub

        Public Shared Function GenerateAutoAssetBarcode(ByVal preview As Boolean, ByVal classifyID As String) As String

            Dim resultBuilder As New LineStrBuilder

            Dim classifyRow As AssetVisionXAuto.FTs.FT_M_AV_ASSET_CLASSIFYRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_ASSET_CLASSIFYRow(classifyID)

            If IsNothing(classifyRow) = True Then
                Return String.Empty
            End If

            Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_ASSET_BARCODE_PREFIX)
            Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_ASSET_BARCODE_DIGIT_LENGTH))
            Dim hasClassifyCode As Boolean = CommTK.FBoolean(SysInfo.ReadShareSysInfo(Decls.SVN_ASSET_BARCODE_HAS_CLASSIFY_CODE))

            Dim codeDuplicate As Boolean = False
            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Do
                codeDuplicate = False

                resultBuilder.Clear()
                resultBuilder.AppendFormat(codePrefix)
                If hasClassifyCode = True Then
                    resultBuilder.AppendFormat(classifyRow.CLASSIFY_CODE)
                End If

                If preview = True Then
                    resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
                    Return resultBuilder.ToString
                End If


                Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_ASSET_BARCODE, resultBuilder.ToString)
                Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

                resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)


                assetCondition.Clear()
                assetCondition.Add(AfBizMaster.T_AV_ASSETColumns.ASSET_BARCODEColumn, "=", resultBuilder.ToString)
                If AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETCount(assetCondition) > 0 Then
                    codeDuplicate = True
                End If


            Loop While codeDuplicate = True

            Return resultBuilder.ToString

        End Function

        Public Shared Function GetAssetAllowedCondition(ByVal loginStaffDepartmentID As String, ByVal loginStaffID As String) As AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_ASSET

            Dim result As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_Or)

            result.Add(AssetVisionXAuto.Facade.AfXV.XV_T_AV_ASSETColumns.CURRENT_DEPARTMENT__DEPARTMENT_IDColumn, "=", loginStaffDepartmentID)
            result.Add(AssetVisionXAuto.Facade.AfXV.XV_T_AV_ASSETColumns.ASSET_KEEPER__STAFF_IDColumn, "=", loginStaffID)
            result.Add(AssetVisionXAuto.Facade.AfXV.XV_T_AV_ASSETColumns.CURRENT_RUNNERColumn, "=", loginStaffID)

            Return result
        End Function

        Public Shared Sub RemoveLocationTreeUnselectedFromLeaf(ByVal taskList As AssetVisionXAuto.FTs.FT_XV_T_AV_INVENTORY_TASK, ByVal parentLocationID As String, ByRef found As Boolean)

            Dim taskCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_INVENTORY_TASK(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim taskRow As AssetVisionXAuto.FTs.FT_XV_T_AV_INVENTORY_TASKRow

            Dim parentRowIDs As New ArrayList
            Dim rowIDsInUse As New List(Of Integer)

            For Each taskRow In taskList.FindRowsSelecting(True)
                parentRowIDs.Add(taskRow.PARENT_ID)
                rowIDsInUse.Add(taskRow.ROW_ID)
            Next


            Do While parentRowIDs.Count > 0
                taskCondition.Clear()
                taskCondition.Add(AfXV.XV_T_AV_INVENTORY_TASKColumns.LOCATION_IDColumn, True, parentRowIDs)

                parentRowIDs = New ArrayList
                For Each taskRow In taskList.FindRowsByCondition(taskCondition)
                    rowIDsInUse.Add(taskRow.ROW_ID)
                    parentRowIDs.Add(taskRow.PARENT_ID)
                Next

            Loop

            For Each taskRow In taskList.FindRowsByCondition(Nothing)
                If rowIDsInUse.Contains(taskRow.ROW_ID) = False Then
                    taskRow.Delete()
                End If
            Next


        End Sub

        Public Shared Function GetASALevelDepartmentID( _
            ByVal currentDepartmentID As String, _
            ByVal currentStaffID As String) As String

            Dim result As String = String.Empty

            Dim departmentRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(currentDepartmentID)

            If IsNothing(departmentRow) = True Then
                Return String.Empty
            End If

            result = currentDepartmentID

            'If departmentRow.DEPARTMENT_DEPTH < asaDepartmentDepth Then
            '    Return currentDepartmentID
            'End If

            Do While departmentRow.DEPARTMENT_DEPTH > AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL
                'If departmentRow.DEPARTMENT_DEPTH = asaDepartmentDepth Then
                '    Return departmentRow.DEPARTMENT_ID
                'End If

                departmentRow = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentRow.PARENT_DEPARTMENT)
                If IsNothing(departmentRow) = True Then
                    Exit Do
                End If
                result = departmentRow.DEPARTMENT_ID
            Loop

            Return result

        End Function

        Public Shared Function GetLocationConditionByStaffID(ByVal staffID As String) As AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION

            Dim result As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            If SysInfo.ReadShareSysInfo(Decls.SVN_ADMIN_ALONE_STAFF_ID) = staffID Then
                Return result
            End If

            Dim staffCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_M_AV_STAFF(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            staffCondition.Add(AfXV.XV_M_AV_STAFFColumns.STAFF_IDColumn, "=", staffID)

            Dim staffRow As AssetVisionXAuto.FTs.FT_XV_M_AV_STAFFRow = AssetVisionXAuto.Facade.AfXV.GetXV_M_AV_STAFFRow(staffCondition)

            If IsNothing(staffRow) = True Then
                Return Nothing
            End If

            If AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL = 0 Then
                Return result
            End If

            Return Facade.OpBizMaster.GetLocationConditionByDepartmentID(staffRow.DEPARTMENT_ID)

        End Function

        Public Shared Function GetLocationConditionByDepartmentID(ByVal departmentID As String) As AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION

            Dim result As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            If AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL = 0 Then
                Return result
            End If

            Dim departmentRow As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentID)

            If IsNothing(departmentRow) = True Then
                result.Clear()
                result.FalseWhileEmpty = True
                Return result
            End If

            If departmentRow.RUNNER.Length = 0 Then
                '当该员工所属部门没有资产管理员, 则看不到任何东西
                result.Clear()
                result.FalseWhileEmpty = True
                Return result
            End If

            Do While departmentRow.DEPARTMENT_DEPTH > AssetVisionXService.Decls.CURRENT_ADDRESS_ALONE_DEPT_LEVEL
                departmentRow = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentRow.PARENT_DEPARTMENT)
            Loop

            Dim locationCondtion As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            locationCondtion.Add(AfBizMaster.M_AV_LOCATIONColumns.DEPARTMENT_IDColumn, "=", departmentRow.DEPARTMENT_ID)
            locationCondtion.Add(AfBizMaster.M_AV_LOCATIONColumns.PARENT_IDColumn, "=", String.Empty)

            Dim rootLocationRow As AssetVisionXAuto.FTs.FT_M_AV_LOCATIONRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_LOCATIONRow(locationCondtion)

            If IsNothing(rootLocationRow) = True Then
                '如果没有当前大部门所管理的顶级地点
                result.FalseWhileEmpty = True
                Return result
            End If

            result.Add(String.Format("LOCATION_ID_PATH Like '{0}%'", rootLocationRow.LOCATION_ID_PATH))

            Return result

        End Function

        Public Shared Function GetLocationAssetRunnerID(ByVal locationID As String) As String

            Dim adminAloneStaffID As String = SysInfo.ReadShareSysInfo(Decls.SVN_ADMIN_ALONE_STAFF_ID)
            If adminAloneStaffID.Length > 0 Then
                Return adminAloneStaffID
            End If
            Dim locationRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_LOCATIONRow(locationID)

            If IsNothing(locationRow) = True Then
                Return String.Empty
            End If

            Dim departmentRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(locationRow.DEPARTMENT_ID)

            If IsNothing(departmentRow) = True Then
                Return String.Empty
            End If

            Return departmentRow.RUNNER

        End Function

        Public Shared Sub FillAssetParentChildsList(ByVal searchAssetCodes As ArrayList, ByRef refAssetList As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSET)

            Dim assetCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim assetRow As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSETRow
            Dim rootAssetRow As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSETRow
            Dim assetIDs As New ArrayList
            Dim rootAssetIDs As New ArrayList

            For Each assetBarCode As String In searchAssetCodes
                assetCondition.Clear()
                assetCondition.Add(AfXV.XV_T_AV_ASSETColumns.ASSET_BARCODEColumn, "=", assetBarCode)

                assetRow = AssetVisionXAuto.Facade.AfXV.GetXV_T_AV_ASSETRow(assetCondition)

                If IsNothing(assetRow) = True Then
                    Continue For
                End If

                rootAssetRow = assetRow


                Do While assetRow.PARENT_ASSET.Length > 0

                    assetCondition.Clear()
                    assetCondition.Add(AfXV.XV_T_AV_ASSETColumns.ASSET_IDColumn, "=", assetRow.PARENT_ASSET)

                    assetRow = AssetVisionXAuto.Facade.AfXV.GetXV_T_AV_ASSETRow(assetCondition)

                    If IsNothing(assetRow) = True Then
                        Exit Do
                    End If

                    rootAssetRow = assetRow

                Loop


                assetIDs.Add(rootAssetRow.ASSET_ID)
                rootAssetIDs.Add(rootAssetRow.ASSET_ID)
            Next

            Dim searchAssetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            Do

                assetCondition.Clear()
                assetCondition.Add(AfXV.XV_T_AV_ASSETColumns.ASSET_IDColumn, True, assetIDs)
                AssetVisionXAuto.Facade.AfXV.FillFT_XV_T_AV_ASSET(assetCondition, refAssetList)

                searchAssetCondition.Clear()
                searchAssetCondition.Add(AfBizMaster.T_AV_ASSETColumns.PARENT_ASSETColumn, True, assetIDs)

                assetIDs = AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSET_CVListDistinct(searchAssetCondition, AfBizMaster.T_AV_ASSETColumns.ASSET_IDColumn)

            Loop While assetIDs.Count > 0


            assetCondition.Clear()
            assetCondition.Add(AfXV.XV_T_AV_ASSETColumns.ASSET_IDColumn, True, rootAssetIDs)
            Dim assetRows = refAssetList.FindRowsByCondition(assetCondition)

            If assetRows.Count = 0 Then
                Dim ex As New XLException(XLException.ErrorClassify.ReadDataError)
                ex.SetMessage(Decls.MSG_ALERT_00024)
                Throw ex
            End If

            For Each assetRow In assetRows
                assetRow.ROW_HIGHLIGHT = Decls.ROW_HIGHLIGHT_CURRENT_ASSET
            Next

        End Sub

        Public Shared Sub CloneAssetInfo(ByVal fromAssetID As String, ByVal underParentID As String, ByVal includeChildAsset As Boolean)

        End Sub

        Public Shared Function ParseAssetBarcodesListArraylistString(ByRef assetBarcodesListString As String) As String

            Dim barcodes As New ArrayList

            Dim splitedBarcodes As String() = assetBarcodesListString.Trim.Split(New Char() {Chr(10), ";"c})

            For Each barcode As String In splitedBarcodes
                If barcode.Trim.Length > 0 Then
                    barcodes.Add(barcode)
                End If
            Next



            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            assetCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.T_AV_ASSETColumns.ASSET_BARCODEColumn, True, barcodes)

            barcodes = _
                AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSET_CVListDistinct( _
                    assetCondition, _
                     AssetVisionXAuto.Facade.AfBizMaster.T_AV_ASSETColumns.ASSET_BARCODEColumn)


            Dim barcodesTextBuilder As New LineStrBuilder
            For Each barcode As String In barcodes
                barcodesTextBuilder.AppendLine(barcode)
            Next

            assetBarcodesListString = barcodesTextBuilder.ToString
            Return CommTK.ALToStr(barcodes)

        End Function

        'The asset id list should be a string transfered from AssetIDs Arraylist
        'And the function returns a string to be transfered into AssetIDs Arraylist
        Public Shared Sub CloneAsset( _
            ByVal parentAssetID As String, _
            ByVal sourceAssetID As String, _
            ByRef createdIDList As ArrayList)

            Dim childAssetIDs As New ArrayList
            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim newAssetID As String
            Dim sourceAssetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow

            sourceAssetRow = AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETRow(sourceAssetID)
            If IsNothing(sourceAssetRow) = True Then
                Return
            End If

            assetCondition.Clear()
            assetCondition.Add(AfBizMaster.T_AV_ASSETColumns.PARENT_ASSETColumn, "=", sourceAssetID)

            childAssetIDs = AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSET_CVListDistinct(assetCondition, AfBizMaster.T_AV_ASSETColumns.ASSET_IDColumn)

            sourceAssetRow.ORIGION_CODE = String.Empty

            sourceAssetRow.ASSET_BARCODE = _
                AssetVisionXService.Facade.OpBizMaster.GenerateAutoAssetBarcode( _
                    False, _
                    sourceAssetRow.CLASSIFY_ID)
            newAssetID = System.Guid.NewGuid.ToString
            sourceAssetRow.ASSET_ID = newAssetID
            sourceAssetRow.PARENT_ASSET = parentAssetID


            AssetVisionXAuto.Facade.AfBizMaster.CreateT_AV_ASSETInfoByRow(sourceAssetRow)

            createdIDList.Add(sourceAssetRow.ASSET_BARCODE)

            For Each childAssetID As String In childAssetIDs
                CloneAsset(newAssetID, childAssetID, createdIDList)
            Next

        End Sub

        ''取到某个职员可以管辖的所有最大的地点ID集合，
        ''为了方便传值起见, 使用String形式存储Arraylist
        'Public Shared Function GetRootGrantedLocationIDStrOfStaff(ByVal staffID As String, ByVal includingRoots As Boolean) As String

        '    Dim resultIDs As New ArrayList


        '    Dim departmentConditions As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_Or)
        '    departmentConditions.Add(AfBizMaster.M_AV_DEPARTMENTColumns.PRINCIPALColumn, "=", staffID)
        '    departmentConditions.Add(AfBizMaster.M_AV_DEPARTMENTColumns.RUNNERColumn, "=", staffID)

        '    Dim departmentIDs As ArrayList = AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENT_CVListDistinct(departmentConditions, AfBizMaster.M_AV_DEPARTMENTColumns.DEPARTMENT_IDColumn)
        '    Dim locationConsitions As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_LOCATION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        '    locationConsitions.Add(AfBizMaster.M_AV_LOCATIONColumns.DEPARTMENT_IDColumn, True, departmentIDs)


        '    Return CommTK.ALToStr(resultIDs)

        'End Function

        Public Shared Function GenerateDepartmentIDPath(ByVal departmentID As String, ByRef departmentDepth As Integer) As String

            Dim result As String = String.Empty
            Dim departmentRow As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRow
            Dim departmentCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim departmentIDPath As New ArrayList

            departmentDepth = 0
            Do While departmentID.Length > 0

                departmentIDPath.Add(departmentID)
                departmentCondition.Clear()
                departmentCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.M_AV_DEPARTMENTColumns.DEPARTMENT_IDColumn, "=", departmentID)
                departmentRow = _
                    AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentCondition)

                If IsNothing(departmentRow) = True Then
                    Return String.Empty
                End If

                departmentDepth += 1
                departmentID = departmentRow.PARENT_DEPARTMENT

            Loop

            Dim counter As Integer

            For counter = departmentIDPath.Count - 1 To 0 Step -1
                result &= CommTK.FString(departmentIDPath(counter)) & "/"
            Next

            result = "/" & result

            Return result

        End Function

        Public Shared Sub SynchronizeStaffWithUser()


            Dim staffList As New AssetVisionXAuto.FTs.FT_M_AV_STAFF
            Dim staffRow As AssetVisionXAuto.FTs.FT_M_AV_STAFFRow
            Dim staffCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_STAFF(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_STAFF(Nothing, staffList)


            Dim userList As New AssetVisionXAuto.FTs.FT_S_USER
            Dim userRow As AssetVisionXAuto.FTs.FT_S_USERRow
            Dim userCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            AssetVisionXAuto.Facade.AfSysConfig.FillFT_S_USER(Nothing, userList)

            Dim secureCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_SECURE(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "=", CommDecl.SYSTEM_DEFAULT_SECURE_NAME)
            Dim defaultSecureRow As AssetVisionXAuto.FTs.FT_S_SECURERow = AssetVisionXAuto.Facade.AfSysConfig.GetS_SECURERow(secureCondition)

            Dim userID As String
            'Dim departmentCodes As New ArrayList
            Dim staffCodes As New ArrayList

            For Each staffRow In staffList

                staffCodes.Add(staffRow.STAFF_CODE)
                userCondition.Clear()
                userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", staffRow.STAFF_CODE)

                userRow = AssetVisionXAuto.Facade.AfSysConfig.GetS_USERRow(userCondition)

                If IsNothing(userRow) = True Then

                    userID = System.Guid.NewGuid.ToString
                    Facade.OpSysConfig.CreateUserInfo( _
                        userID, _
                        staffRow.STAFF_CODE, _
                        staffRow.STAFF_CODE, _
                        String.Empty, _
                        String.Empty)

                Else
                    userID = userRow.USER_ID
                End If

                AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_STAFFRow(staffRow.STAFF_ID, AfBizMaster.M_AV_STAFFColumns.USER_IDColumn, userID)
            Next

            'secureCondition.Clear()
            'secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, False, departmentCodes)
            'secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "<>", CommDecl.SYSTEM_DEFAULT_SECURE_NAME)
            'AssetVisionXAuto.Facade.AfSysConfig.DeleteS_SECUREData(secureCondition)

            userCondition.Clear()
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, False, staffCodes)
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "<>", CommDecl.SYSTEM_DEFAULT_ACCOUNT)

            AssetVisionXAuto.Facade.AfSysConfig.DeleteS_USERData(userCondition)

        End Sub


        'Public Shared Sub SynchronizeDepartmentStaffWithSecureUser()

        '    Dim departmentList As New AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENT
        '    Dim departmentRow As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRow
        '    Dim departmentCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        '    AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_DEPARTMENT(Nothing, departmentList)

        '    Dim staffList As New AssetVisionXAuto.FTs.FT_M_AV_STAFF
        '    Dim staffRow As AssetVisionXAuto.FTs.FT_M_AV_STAFFRow
        '    Dim staffCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_STAFF(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        '    AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_STAFF(Nothing, staffList)

        '    Dim secureList As New AssetVisionXAuto.FTs.FT_S_SECURE
        '    Dim secureRow As AssetVisionXAuto.FTs.FT_S_SECURERow
        '    Dim secureCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_SECURE(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        '    AssetVisionXAuto.Facade.AfSysConfig.FillFT_S_SECURE(Nothing, secureList)

        '    Dim userList As New AssetVisionXAuto.FTs.FT_S_USER
        '    Dim userRow As AssetVisionXAuto.FTs.FT_S_USERRow
        '    Dim userCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        '    AssetVisionXAuto.Facade.AfSysConfig.FillFT_S_USER(Nothing, userList)

        '    Dim secureID As String
        '    Dim userID As String
        '    Dim departmentCodes As New ArrayList
        '    Dim staffCodes As New ArrayList
        '    For Each departmentRow In departmentList

        '        departmentCodes.Add(departmentRow.DEPARTMENT_CODE)

        '        secureCondition.Clear()
        '        secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "=", departmentRow.DEPARTMENT_CODE)
        '        secureRow = AssetVisionXAuto.Facade.AfSysConfig.GetS_SECURERow(secureCondition)

        '        If IsNothing(secureRow) = True Then
        '            secureID = System.Guid.NewGuid.ToString
        '            Facade.OpSysConfig.CreateSecureInfo( _
        '                secureID, _
        '                departmentRow.DEPARTMENT_CODE, _
        '                String.Empty)
        '        Else
        '            secureID = secureRow.SECURE_ID
        '        End If

        '        AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_DEPARTMENTRow(departmentRow.DEPARTMENT_ID, AfBizMaster.M_AV_DEPARTMENTColumns.SECURE_IDColumn, secureID)

        '        staffCondition.Clear()
        '        staffCondition.Add(AfBizMaster.M_AV_STAFFColumns.DEPARTMENT_IDColumn, "=", departmentRow.DEPARTMENT_ID)

        '        For Each staffRow In staffList.FindRowsByCondition(staffCondition)

        '            staffCodes.Add(staffRow.STAFF_CODE)
        '            userCondition.Clear()
        '            userCondition.Add(AfSysConfig.S_USERColumns.SECURE_IDColumn, "=", secureID)
        '            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", staffRow.STAFF_CODE)

        '            userRow = AssetVisionXAuto.Facade.AfSysConfig.GetS_USERRow(userCondition)

        '            If IsNothing(userRow) = True Then
        '                userID = System.Guid.NewGuid.ToString
        '                Facade.OpSysConfig.CreateUserInfo( _
        '                    userID, _
        '                    staffRow.STAFF_CODE, _
        '                    staffRow.STAFF_CODE, _
        '                    secureID, _
        '                    String.Empty)
        '            Else
        '                userID = userRow.USER_ID
        '            End If

        '            AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_STAFFRow(staffRow.STAFF_ID, AfBizMaster.M_AV_STAFFColumns.USER_IDColumn, userID)
        '        Next

        '    Next


        '    secureCondition.Clear()
        '    secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, False, departmentCodes)
        '    secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "<>", CommDecl.SYSTEM_DEFAULT_SECURE_NAME)
        '    AssetVisionXAuto.Facade.AfSysConfig.DeleteS_SECUREData(secureCondition)

        '    userCondition.Clear()
        '    userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, False, staffCodes)
        '    userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "<>", CommDecl.SYSTEM_DEFAULT_ACCOUNT)

        '    AssetVisionXAuto.Facade.AfSysConfig.DeleteS_USERData(userCondition)

        'End Sub

        Public Shared Function ValidateStaffPassword(ByVal staffCode As String, ByVal password As String) As Boolean


            Dim userCondition As New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", staffCode)

            Dim userRow As AssetVisionXAuto.FTs.FT_S_USERRow = _
                AssetVisionXAuto.Facade.AfSysConfig.GetS_USERRow(userCondition)

            If IsNothing(userRow) = True Then
                Return False
            End If

            If Security.Encrypt(password) <> userRow.USER_PASSWORD Then
                Return False
            End If

            Return True
        End Function

        Public Shared Sub FillDepartmentListOfRunner( _
            ByVal staffID As String, _
            ByRef departmentList As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENT)

            Dim departmentCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            departmentCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.M_AV_DEPARTMENTColumns.RUNNERColumn, "=", staffID)

            AssetVisionXAuto.Facade.AfBizMaster.FillFT_M_AV_DEPARTMENT(departmentCondition, departmentList)
        End Sub

        'Public Shared Function GetAutoStaffCode(ByVal preview As Boolean, ByVal departmentID As String) As String

        '    Dim resultBuilder As New LineStrBuilder

        '    Dim departmentRow As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRow = _
        '        AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentID)

        '    If IsNothing(departmentRow) = True Then
        '        Return String.Empty
        '    End If

        '    Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_STAFF_CODE_PREFIX)
        '    Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_STAFF_CODE_LENGTH))
        '    'Dim hasClassifyCode As Boolean = CommTK.FBoolean(SysInfo.ReadShareSysInfo(Decls.SVN_ASSET_BARCODE_HAS_CLASSIFY_CODE))

        '    resultBuilder.AppendFormat(codePrefix)
        '    'If hasClassifyCode = True Then
        '    resultBuilder.AppendFormat(departmentRow.DEPARTMENT_CODE)
        '    'End If

        '    If preview = True Then
        '        resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
        '        Return resultBuilder.ToString
        '    End If

        '    Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_STAFF_CODE, resultBuilder.ToString)
        '    Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

        '    resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)

        '    Return resultBuilder.ToString

        'End Function




    End Class
End Namespace

